﻿using System;
using System.Collections.Generic;
using System.Text;

using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;

using ISS.CaseStudy.DBUtility;
using ISS.CaseStudy.Entities;

namespace ISS.CaseStudy.DAL
{
    public static class ClientInfoDLL
    {
        public static DataSet GetAllInfos()
        {
            string sql = "select * from ClientManage order by ModifyTime desc";
            DataSet dataSet = Database.SpExecuteDataSet(sql);
            return dataSet;
        }

        public static ClientInfo GetInfo(string id)
        {
            string sql = "select * from ClientManage where [Id]=@id";
            SqlParameter[] paramCollection = { new SqlParameter("@id", id) };
            DataSet dataSet = Database.SpExecuteDataSet(sql, paramCollection);
            DataTable dataTable = dataSet.Tables[0];
            if (dataTable.Rows.Count == 0)
            {
                return null;
            }

            DataRow row = dataSet.Tables[0].Rows[0];

            ClientInfo clientInfo = new ClientInfo();
            clientInfo.Id = id;
            clientInfo.Value = row["Value"].ToString();

            if (!string.IsNullOrEmpty(row["CreateTime"].ToString()))
            {
                clientInfo.CreateTime = DateTime.Parse(row["CreateTime"].ToString());
            }

            if (!string.IsNullOrEmpty(row["ModifyTime"].ToString()))
            {
                clientInfo.ModifyTime = DateTime.Parse(row["ModifyTime"].ToString());
            }

            return clientInfo;
        }

        public static bool Update(ClientInfo clientInfo)
        {
            bool b = false;
            string sql = "UPDATE ClientManage SET [Value]=@Value, [ModifyTime]=@ModifyTime where ([Id]=@Id)";
            SqlParameter[] paramCollection = { new SqlParameter("@Value", clientInfo.Value), 
                                                 new SqlParameter("@ModifyTime", clientInfo.ModifyTime.ToString()),
                                                 new SqlParameter("@Id", clientInfo.Id)};
            int i = Database.SpExecuteNonQuery(sql, paramCollection);
            if (i > 0)
            {
                b = true;
            }
            return b;
        }

        public static bool Insert(ClientInfo clientInfo)
        {
            bool b = false;
            string sql = "insert into ClientManage([Value], [CreateTime], [ModifyTime]) values (@Value, @CreateTime, @ModifyTime)";
            SqlParameter[] paramCollection = { new SqlParameter("@Value", clientInfo.Value), 
                                                 new SqlParameter("@CreateTime", clientInfo.CreateTime.ToString()), 
                                                 new SqlParameter("@ModifyTime", clientInfo.ModifyTime.ToString()),};
            int i = Database.SpExecuteNonQuery(sql, paramCollection);
            if (i > 0)
            {
                b = true;
            }
            return b;
        }

        public static bool Delete(string id)
        {
            bool b = false;
            string sql = "delete from ClientManage where ([Id]=@Id)";
            SqlParameter[] paramCollection = { new SqlParameter("@Id", id) };
            int i = Database.SpExecuteNonQuery(sql, paramCollection);
            if (i > 0)
            {
                b = true;
            }
            return b;
        }
    }
}
